package com.bobby.web;

import com.bobby.pojo.Book;
import com.bobby.pojo.Cart;
import com.bobby.pojo.CartItem;
import com.bobby.service.BookService;
import com.bobby.service.impl.BookServiceImpl;
import com.bobby.utils.WebUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/8/16 9:50
 * @Created wenjunpei
 */
public class CartServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();


    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cart cart = WebUtils.getCart(request);
        String idStr = request.getParameter("id");
        Book book = bookService.queryBookById(Integer.parseInt(idStr));
        cart.addItem(new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice()));
        request.getSession().setAttribute("lastName",book.getName());
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cart cart = WebUtils.getCart(request);
        String idStr = request.getParameter("id");
        Integer id = Integer.parseInt(idStr);
        cart.deleteItem(id);
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void clear(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cart cart = WebUtils.getCart(request);
        cart.clear();
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Cart cart = WebUtils.getCart(request);
        String idStr = request.getParameter("id");
        String countStr = request.getParameter("count");
        Integer id = Integer.parseInt(idStr);
        Integer count = Integer.parseInt(countStr);
        cart.updateCount(id, count);
        response.sendRedirect(request.getHeader("Referer"));
    }

//    protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        Cart cart = WebUtils.getCart(request);
//        String idStr = request.getParameter("id");
//        Book book = bookService.queryBookById(Integer.parseInt(idStr));
//        cart.addItem(new CartItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice()));
//
//
//
//        Integer totalCount = cart.getTotalCount();
//        String lastName = book.getName();
//
//        request.getSession().setAttribute("lastName",lastName);
//
//        Map<String,Object> map = new HashMap<>();
//        map.put("totalCount",totalCount);
//        map.put("lastName",lastName);
//        Gson gson = new Gson();
//        String mapJsonString = gson.toJson(map);
//        response.getWriter().write(mapJsonString);
//    }
}
